﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace ClassAdd
{
    class XMLParsing
    {

        XmlDocument doc = new XmlDocument();

        public void LoadXML(String pathToXML, String pathToWrite)
        {
            doc.LoadXml(File.ReadAllText(pathToXML));
            XmlNodeList classList = doc.DocumentElement.ChildNodes;

            foreach (XmlNode cList in classList)
            {
#region ModelGeneation
                //генерация класса модели
                if (cList.Attributes[0].Value == "Model")
                {
                    ModelClassDescription modelClassDesciption = new ModelClassDescription();
                    foreach (XmlNode childList in cList)
                    {
                        if (childList.Name == "class")
                        {
                            foreach (XmlNode attributesClass in childList)
                            {
                                if (attributesClass.Name == "attribute")
                                    modelClassDesciption.attribyteClass = modelClassDesciption.attribyteClass + attributesClass.InnerText + "\n";
                            }
                            modelClassDesciption.nameClass += childList.Attributes[0].Value;
                        }

                            

                        if (childList.Name == "namespace")
                            modelClassDesciption.nameSpace = childList.InnerText;

                        if (childList.Name == "using_list")
                        {
                            foreach (XmlNode uList in childList)
                            {
                                modelClassDesciption.usingList = modelClassDesciption.usingList + uList.Name + " " + uList.InnerText + "\n";
                            }
                        }

                        if (childList.Name == "variable")
                        {
                            foreach (XmlNode attributeList in childList)
                            {
                                if (attributeList.Name == "attribute")
                                {
                                    modelClassDesciption.variable = modelClassDesciption.variable + attributeList.InnerText + "\n";
                                }
                            }

                            foreach (XmlNode variableMod in childList)
                            {
                                if (variableMod.Name == "mod")
                                    modelClassDesciption.variable = modelClassDesciption.variable + variableMod.InnerText + " ";
                            }

                            foreach (XmlNode variableType in childList)
                            {
                                if (variableType.Name == "type")
                                    modelClassDesciption.variable = modelClassDesciption.variable + variableType.InnerText + " ";
                            }

                            modelClassDesciption.variable = modelClassDesciption.variable + childList.Attributes[0].Value + ";" + "\n";

                        }

                        if (childList.Name == "function")
                        {
                            foreach (XmlNode functionMod in childList)
                            {
                                if (functionMod.Name == "mod")
                                    modelClassDesciption.functionList = modelClassDesciption.functionList + functionMod.InnerText + " ";
                            }

                            foreach (XmlNode functionType in childList)
                            {
                                if (functionType.Name == "type")
                                {
                                    modelClassDesciption.functionList = modelClassDesciption.functionList + functionType.InnerText + " ";
                                }
                            }

                            modelClassDesciption.functionList = modelClassDesciption.functionList + childList.Attributes[0].Value + "(";

                            foreach (XmlNode functionInput in childList)
                            {
                                if (functionInput.Name == "variable_input")
                                    modelClassDesciption.functionList = modelClassDesciption.functionList + functionInput.Attributes[0].Value 
                                        + " " + functionInput.InnerText + ", ";
                            }
                            
                            modelClassDesciption.functionList = modelClassDesciption.functionList.Insert(modelClassDesciption.functionList.Length - 2, ")");
                            //костыль для удаления последнего символа в строке
                            String tempString = modelClassDesciption.functionList;
                            modelClassDesciption.functionList = tempString.Remove(tempString.Length - 2);                           
                            
                            modelClassDesciption.functionList = modelClassDesciption.functionList + "\n" + "{" + "\n";

                            foreach (XmlNode functionValues in childList)
                            {
                                if (functionValues.Name == "function_variable")
                                    modelClassDesciption.functionList = modelClassDesciption.functionList + functionValues.Attributes[0].Value 
                                        + " " + functionValues.InnerText + ";\n";
                            }

                            foreach (XmlNode functionReturn in childList)
                            {
                                if (functionReturn.Name == "return")
                                    modelClassDesciption.functionList = modelClassDesciption.functionList + "return " + functionReturn.InnerText 
                                        + ";\n}\n";

                            }

                        }
                    }

                    modelClassDesciption.writeClass(pathToWrite);
                    
                }
#endregion

#region ContollerGenerator
                //генерация класса контроллера
                if (cList.Attributes[0].Value == "Controller")
                {
                    ControlClassDescription controlClassDescription = new ControlClassDescription();
                    foreach (XmlNode childList in cList)
                    {
                        if (childList.Name == "using_list")
                        {
                            foreach (XmlNode uList in childList)
                            {
                                controlClassDescription.usingList = controlClassDescription.usingList + uList.Name + " " + uList.InnerText + "\n";
                            }
                        }

                        if (childList.Name == "namespace")
                            controlClassDescription.nameSpace = childList.InnerText;

                        if (childList.Name == "class")
                        {
                            foreach (XmlNode attributeList in childList)
                            {
                                if (attributeList.Name == "attribute")
                                {
                                    controlClassDescription.attrubuteClass += attributeList.InnerText + "\n";
                                }
                            }

                            controlClassDescription.nameClass = childList.Attributes[0].Value;
                        }

                        if (childList.Name == "variable")
                        {
                            foreach (XmlNode attributeList in childList)
                            {
                                if (attributeList.Name == "attribute")
                                {                                    
                                    controlClassDescription.variable += attributeList.InnerText + "\n";
                                }
                            }

                            foreach (XmlNode variableMod in childList)
                            {
                                if (variableMod.Name == "mod")
                                    controlClassDescription.variable += variableMod.InnerText + " ";
                            }

                            foreach (XmlNode variableType in childList)
                            {
                                if (variableType.Name == "type")
                                    controlClassDescription.variable += variableType.InnerText + " ";
                            }

                            controlClassDescription.variable += childList.Attributes[0].Value + ";" + "\n";
                        }

                        if (childList.Name == "function")
                        {
                            foreach (XmlNode attributeList in childList)
                            {
                                if (attributeList.Name == "attribute")
                                {
                                    controlClassDescription.functionList += attributeList.InnerText + "\n";
                                }
                            }

                            foreach (XmlNode functionMod in childList)
                            {
                                if (functionMod.Name == "mod")
                                    controlClassDescription.functionList += functionMod.InnerText + " ";
                            }

                            foreach (XmlNode functionType in childList)
                            {
                                if (functionType.Name == "type")
                                {
                                    controlClassDescription.functionList += functionType.InnerText + " ";
                                }
                            }

                            controlClassDescription.functionList += childList.Attributes[0].Value + "(";

                            foreach (XmlNode functionInput in childList)
                            {
                                if (functionInput.Name == "variable_input")
                                    controlClassDescription.functionList += functionInput.Attributes[0].Value + " " + functionInput.InnerText + ", ";
                            }
                            controlClassDescription.functionList = controlClassDescription.functionList.Insert(controlClassDescription.functionList.Length - 2, ")");
                            //костыль для удаления одного символа с конца строки
                            String tempString = controlClassDescription.functionList;
                            controlClassDescription.functionList = tempString.Remove(tempString.Length - 2);
                            controlClassDescription.functionList += "\n" + "{" + "\n";

                            foreach (XmlNode functionValues in childList)
                            {
                                if (functionValues.Name == "function_variable")
                                    controlClassDescription.functionList += functionValues.Attributes[0].Value + " " + functionValues.InnerText 
                                        + ";\n";
                            }

                            foreach (XmlNode functionReturn in childList)
                            {
                                if (functionReturn.Name == "return")
                                    controlClassDescription.functionList += "return " + functionReturn.InnerText + ";\n}\n";

                            }

                        }



                    }

                    controlClassDescription.writeClass(pathToWrite);
                }
#endregion


            }

        }


    }

}
